/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkgjeudestrategie;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

/**
 *
 * @author Admin
 */
public class Fiche extends JFrame {

    static private Toolkit toolkit = Toolkit.getDefaultToolkit();
    static final Dimension scrnsize = toolkit.getScreenSize();
    static final int tailleX = (int) scrnsize.getWidth();
    static final int tailleY = (int) scrnsize.getHeight() - 40;
    int name = 0;
    int revenu = 0;
    int pop = 0;
    int happy = 0;
    int soldier = 0;
    JTabbedPane jtbOnglets = new JTabbedPane();
    JPanel pnlUn = new JPanel(new BorderLayout());
    JPanel pnlDeux = new JPanel();
    JLabel lblTax = new JLabel("Taux d'imposition");
    String[] choix = {"10", "20", "30", "40", "50"};
    JLabel lblTaxSuite = new JLabel("%");
    JButton btnNom = new JButton("Nom de la ville");
    JButton btnRevenu = new JButton("Revenu par tour ($)");
    JButton btnPopulation = new JButton("Nombre de citoyens");
    JButton btnBonheur = new JButton("Bonheur des citoyens (%)");
    JButton btnSoldat = new JButton("Nombre de soldats");
    JLabel lblSoldierTot = new JLabel("Nombre total de soldats: ");
    JLabel lblRevenuTot = new JLabel("Revenus totaux: ");
    JLabel lblCityTot = new JLabel("Nombre total de villes: ");
    Faction f;
    JLabel lblDeux = new JLabel("Panneau et étiquette 2");
    JComboBox cboBoite = new JComboBox(choix);
    JTextArea txtScroll = new JTextArea("yo");
    JScrollPane cboScroller = new JScrollPane(txtScroll);

           public Fiche(Faction f)
{
super("Écran d'informations");txtScroll.setEnabled(false);
txtScroll.setBackground(Color.red);
this.f=f;
ArrayList<Territoire> territoireList=f.getTerritoireList();
changeText(territoireList);
actualizeLabels();
cboBoite.setSelectedIndex(territoireList.get(0).getTaxePosition());
JPanel pnlGrid=new JPanel(new GridLayout(0,3));
JPanel pnlGrid2=new JPanel(new GridLayout(0,5));
JPanel pnlGrid3=new JPanel(new GridLayout(0,2));
JPanel pnlGrid4=new JPanel(new GridLayout(3,0));
pnlGrid2.add(btnNom);
pnlGrid2.add(btnRevenu);
pnlGrid2.add(btnPopulation);
pnlGrid2.add(btnBonheur);
pnlGrid2.add(btnSoldat);
pnlDeux.add(new EcranScience(f));
cboBoite.setPreferredSize(new Dimension(50, 20));
pnlGrid.add(lblTax);
pnlGrid.add(cboBoite);
pnlGrid.add(lblTaxSuite);
pnlGrid4.add(lblSoldierTot);
pnlGrid4.add(lblRevenuTot);
pnlGrid4.add(lblCityTot);
pnlGrid3.add(pnlGrid4);
pnlGrid3.add(pnlGrid);
pnlUn.add( pnlGrid2,BorderLayout.NORTH);
pnlUn.add( pnlGrid3,BorderLayout.SOUTH);
pnlUn.add(cboScroller,BorderLayout.CENTER);
jtbOnglets.addTab("Informations générales", pnlUn);
jtbOnglets.setSelectedIndex(0); // Ouvrir sur le 1er onglet
jtbOnglets.addTab("Recherche", pnlDeux);
this.add(jtbOnglets);
listen();
this.setSize(950,600);
this.setVisible(true);
setLocation((int)((tailleX/2)-(getWidth()/2)),(int)( (tailleY/2)-(getHeight()/2)));
}
           
    public void actualizeLabels() {
        ArrayList<Territoire> territoireList = f.getTerritoireList();
        int soldat = 0;
        int villes = territoireList.size();
        int revenu = 0;
        for (Territoire t : territoireList) {
            soldat = soldat + t.getSoldatNb();
            revenu = revenu + t.getRevenuTerritoire(f);
        }
        lblSoldierTot.setText("Nombre total de soldats: " + soldat);
        lblRevenuTot.setText("Revenus totaux: " + revenu);
        lblCityTot.setText("Nombre total de villes: " + villes);
    }

    public void changeText(ArrayList<Territoire> lst) {
        String s = "";
        for (Territoire t : lst) {
            s = s + getCentered(t.getName());
            s = s + getCentered("" + t.getRevenuTerritoire(f));
            s = s + getCentered("" + t.getPopulation());
            s = s + getCentered("" + t.getHapiness(f));
            s = s + getCentered("" + t.getSoldatNb()) + "\n";
        }
        txtScroll.setText(s);
    }

    public String getCentered(String s) {
        String s2 = "";
        String s3 = "";
        int x = s.length();
        for (int y = 0; y < (int) (28 - (x / 2)); y++) {
            s3 = s3 + " ";
        }
        s2 = s3 + s + s3;

        return s2;
    }

    public ArrayList<Territoire> triSelection(Territoire[] tab, int tri) {
        int positionMin;
        Territoire temp;
        ArrayList<Territoire> lst = new ArrayList();
        for (int i = 0; i < tab.length - 1; i++) {
            // tab.length-1 puisque le dernier élément sera le plus grand, donc
            // il sera trié automatiquement
            positionMin = i;

            for (int j = i + 1; j < tab.length; j++) {
                // Recherche du plus petit élément dans le reste du tableauelse
                if (tri == 1 || tri == -1) {
                    if (tab[j].getName().charAt(0) < tab[positionMin].getName().charAt(0)) {
                        positionMin = j;
                    } else if (tab[j].getName().charAt(0) == tab[positionMin].getName().charAt(0)) {
                        String s = tab[j].getName();
                        String s2 = tab[positionMin].getName();
                        String s3 = "";
                        if (s.length() < s2.length()) {
                            s3 = s2;
                        } else {
                            s3 = s;
                        }
                        boolean eternity = true;
                        int y = 0;
                        while (eternity) {
                            if (tab[j].getName().charAt(y) < tab[positionMin].getName().charAt(y)) {
                                positionMin = j;
                                eternity = false;
                            }
                            if (y == s3.length()) {
                                eternity = false;
                            }
                            y++;
                        }
                    }
                } else if (tri == 2 || tri == -2) {
                    if (tab[j].getRevenuTerritoire(f) < tab[positionMin].getRevenuTerritoire(f)) {
                        positionMin = j;
                    }
                } else if (tri == 3 || tri == -3) {
                    if (tab[j].getPopulation() < tab[positionMin].getPopulation()) {
                        positionMin = j;
                    }
                } else if (tri == 4 || tri == -4) {
                    if (tab[j].getHapiness(f) < tab[positionMin].getHapiness(f)) {
                        positionMin = j;
                    }
                } else if (tri == 5 || tri == -5) {
                    if (tab[j].getSoldatNb() < tab[positionMin].getSoldatNb()) {
                        positionMin = j;
                    }
                }
            }
            // Échanger le premier élément non trié et le plus petit élément
            temp = tab[positionMin];
            tab[positionMin] = tab[i];
            tab[i] = temp;
        }
        if (tri > 0) {
            for (int x = 0; x < tab.length; x++) {
                lst.add(tab[x]);
            }
        } else {
            for (int x = tab.length - 1; x >= 0; x--) {
                lst.add(tab[x]);
            }
        }
        return lst;
    }

    public Territoire[] listToTab(ArrayList<Territoire> lst) {
        Territoire[] t = new Territoire[lst.size()];
        for (int x = 0; x < lst.size(); x++) {
            t[x] = lst.get(x);
        }
        return t;
    }

    public void changeSorting(int i) {
        ArrayList<Territoire> tempList = new ArrayList();
        ArrayList<Territoire> territoireList = f.getTerritoireList();
        tempList = triSelection(listToTab(territoireList), i);
        changeText(tempList);



        //t.nom);
        // t.getRevenuTerritoire()
        //t.getPopulation()
        // t.getHapiness()
        // t.getSoldatNb()
    }

    public void setInts(int i1, int i2, int i3, int i4, int i5) {
        name = i1;
        revenu = i2;
        pop = i3;
        happy = i4;
        soldier = i5;
    }

    public void setAction(int i, int position) {
        //+ pour décroissant, - pour croissant, position pour connaitre la mesure
        int sorting = 1;
        if (i == 1) {
            i = -1;
        } else {
            i = 1;
        }

        sorting = position * i;

        if (position == 1) {
            setInts(i, 0, 0, 0, 0);
        } else if (position == 2) {
            setInts(0, i, 0, 0, 0);
        } else if (position == 3) {
            setInts(0, 0, i, 0, 0);
        } else if (position == 4) {
            setInts(0, 0, 0, i, 0);
        } else if (position == 5) {
            setInts(0, 0, 0, 0, i);
        }


        changeSorting(sorting);
    }

    public void listen() {
        btnNom.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                setAction(name, 1);
            }
        });
        btnRevenu.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                setAction(revenu, 2);

            }
        });
        btnPopulation.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                setAction(pop, 3);

            }
        });
        btnBonheur.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                setAction(happy, 4);

            }
        });
        btnSoldat.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                setAction(soldier, 5);

            }
        });
        cboBoite.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                String s = (String) cboBoite.getSelectedItem();
                ArrayList<Territoire> territoireList = f.getTerritoireList();
                for (Territoire t : territoireList) {
                    t.setTaxPercent(Double.parseDouble(s),f);
                }
                actualizeLabels();
                changeText(territoireList);
            }
        });
    }
}
